import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
import Dashboard from '@/components/Dashboard'
import Jobinfos from '@/components/jobmanager/Jobinfos'
import Jobexecutors from '@/components/executormanager/Jobexecutors'
import Joblogs from '@/components/logmanager/Joblogs'
import JobCode from '@/components/JobCode'
// 懒加载方式，当路由被访问的时候才加载对应组件
const Login = resolve => require(['@/components/Login'], resolve)

Vue.use(Router)

let router = new Router({
// mode: 'history',
  routes: [
    {
      path: '/login',
      name: '登录',
      component: Login
    },
    // {
    //   path: '/jobcode',
    //   name: '代码编辑',
    //   component: JobCode
    // },
    {
      path: '/',
      name: 'home',
      component: Home,
      redirect: '/dashboard',
      leaf: true, // 只有一个节点
      menuShow: true,
      iconCls: 'iconfont icon-wxbbaobiao', // 图标样式class
      children: [
        {path: '/dashboard', component: Dashboard, name: '运行报表', menuShow: true}
      ]
    },
    {
      path: '/jobmanager',
      component: Home,
      name: '任务管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'iconfont icon-iconfontshipin', // 图标样式class
      children: [
        {path: '/jobinfos', component: Jobinfos, name: '任务管理', menuShow: true},
        {path: '/jobcode', component: JobCode, name: '代码编辑', menuShow: false}
      ]
    },
    {
      path: '/executormananger',
      component: Home,
      name: '执行器管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'iconfont icon-iconfontqicheyongpin',
      children: [
        {path: '/jobexecutor', component: Jobexecutors, name: '执行器管理', menuShow: true}
      ]
    },
    {
      path: '/logmananger',
      component: Home,
      name: '日志管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'iconfont icon-text',
      children: [
        {path: '/joblog', component: Joblogs, name: '日志管理', menuShow: true}
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  // console.log('to:' + to.path)
  if (to.path.startsWith('/login')) {
    window.localStorage.removeItem('access-user')
    next()
  } else {
    let user = JSON.parse(window.localStorage.getItem('access-user'))
    if (!user) {
      next({path: '/login'})
    } else {
      next()
    }
  }
})

export default router
